<template>
  <!-- // 分配角色 -->
  <div>
    <el-checkbox-group v-model="roleIds">
      <el-checkbox v-for="item in list" :key="item.id" :label="item.id">
        {{ item.name }}
      </el-checkbox>
    </el-checkbox-group>
    <div style="margin-top: 20px; text-align: right">
      <el-button type="primary" @click="hSave">确定</el-button>
      <el-button @click="closeDialog">取消</el-button>
    </div>
  </div>
</template>
<script>
import { getUserDetailById } from '@/api/user.js'
import { getRoles } from '@/api/setting.js'
import { assignRoles } from '@/api/employes'
export default {
  name: 'AssignRole',
  props: {
    curid: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      roleIds: [],
      list: []
    }
  },
  created() {
    // 获取所有角色
    this.loadRoles()
    // 获取员工本来的角色
    this.loadUserDetailById()
  },
  methods: {
    closeDialog() {
      this.$emit('Cancel')
    },
    // 获取所有角色
    async loadRoles() {
      try {
        const { data } = await getRoles({ page: 1, pagesize: 99999 })
        this.list = data.rows
      // 保存数据
      } catch (err) {
        console.log(err)
      }
    },
    // 获取员工本来的角色
    async loadUserDetailById() {
      try {
        const { data } = await getUserDetailById(this.curid)
        // console.log(res)
        this.roleIds = data.roleIds
      } catch (err) {
        console.log(err)
      }
    },
    // 更新角色权限
    hSave() {
      this.doSave()
    },
    // 更新角色权限
    async doSave() {
      try {
        await assignRoles({ id: this.curid, roleIds: this.roleIds })
        this.$message.success('更新角色权限成功')
        this.$emit('Cancel')
      // 保存数据
      } catch (err) {
        console.log(err)
      }
    }
  }
}
</script>
